<template>
	<view class="productItem" @click="goDetail">
		<view class="user-info">
			<!-- <uni-icons type="contact" class="icon" size="26" color="#222"></uni-icons> -->
			<view class="name">
				{{ source.supplierName }}
			</view>
			<uni-icons type="phone" v-if="source.serviceTel" size="18" color="#666"></uni-icons>
			<view class="tel" @click.stop="callPhone(source.supplierTel)">
				{{ source.serviceTel }}
			</view>
		</view>
		<view class="region">
			{{ source.businessAddress }}
		</view>
		<view class="info-wrapper" v-if="index == '0'">
			<view class="item flex">
				<view>
					订货商品：{{ source.orderCategoryQuantity }}个
				</view>
				<view class="item-bottom" :class="{'warning':source.pendingCategoryQuantity>0}">
					{{dateStr}}商品：{{ source.pendingCategoryQuantity }}个
				</view>
			</view>
			<view class="item">
				<view>
					订货件数：{{ source.orderQuantity }}件
				</view>
				<view class="item-bottom" :class="{'warning':source.pendingCategoryQuantity>0}">
					{{dateStr}}件数：{{ source.pendingQuantity }}件
				</view>
			</view>
			<uni-icons type="right" color="#898989" size="18"></uni-icons>
		</view>
		<view class="info-wrapper" v-else>
			<view class="item flex">
				<view>
					订货商品：{{ source.orderCategoryQuantity }}个
				</view>
				<view class="item-bottom">
					已交货商品：{{ source.deliveryCategoryQuantity }}个
				</view>
			</view>
			<view class="item">
				<view>
					订货件数：{{ source.orderQuantity }}件
				</view>
				<view class="item-bottom">
					已交货件数：{{ source.deliveryQuantity }}件
				</view>
			</view>
			<uni-icons type="right" color="#898989" size="18"></uni-icons>
		</view>
		<view class="btn-wrapper">
			<view class="date">
				{{ source.schemaCaption }}
			</view>
			<view v-if="source.status === 0 && index == '0'" class="tip">
				结单后才能打印
			</view>
			<view v-if="showPrint && source.status > 0 && index == '0'" class="btn" @click.stop="remind">
				提醒交货
			</view>
			<view v-if=" index == '0' && showPrint && source.status > 0" class="btn primary" @click.stop="print">
				打印供货单
			</view>
			<!-- <view v-if="printDisabled && index == '0'" class="date">
				截单后才能打印
			</view> -->
			<!-- <view v-if="printDisabled && index == '0'" class="btn disabled" @click.stop="empty">
				打印供货单
			</view> -->
			<view v-if="history || index == '1'" class="btn" @click.stop="goTesting">
				质检记录
			</view>
			<view v-if="history || index == '1'" class="btn" @click.stop="goReceipt">
				交货记录
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			index: {
				type: Number,
				default () {
					return 0
				}
			},
			source: {
				type: Object,
				default () {
					return {}
				}
			}
		},
		data() {
			return {

			}
		},
		computed: {
			supplyWarehouseInfo() {
				return this.$store.state.userInfo.supplyWarehouseInfo
			},
			//截单时间
			deadlineOrders() {
				return this.supplyWarehouseInfo.deadlineOrders.split(":")[0]
			},
			//开市时间
			openingTime() {
				return this.supplyWarehouseInfo.openingTime.split(":")[0]
			},
			//截止交货时间
			deadlineReceiving() {
				return this.supplyWarehouseInfo.deadlineReceiving.split(":")[0]
			},
			printDisabled() {
				const nowTime = new Date().getHours();
				const tomorrow = this.$helper.getDay(1, '-')
				const today = this.$helper.getDay(0, '-')
				const supplyDate = this.source.supplyDate.substring(0, 10)
				if (supplyDate === today) {
					return nowTime < this.deadlineOrders;
				} else if (supplyDate === tomorrow) {
					return true
				} else {
					return false
				}
			},
			showPrint() {
				const nowTime = new Date().getHours();
				const today = this.$helper.getDay(0, '-')
				const tomorrow = this.$helper.getDay(1, '-')
				const supplyDate = this.source.supplyDate.substring(0, 10)
				if (supplyDate === today) { //如果选中今天
					//return nowTime >= this.deadlineOrders;
					return true;
				} else {
					return false;
				}
			},
			history() {
				const today = this.$helper.getDay(0, '-')
				const tomorrow = this.$helper.getDay(1, '-')
				const supplyDate = this.source.supplyDate.substring(0, 10)
				return supplyDate !== today && supplyDate !== tomorrow
			},
			dateStr() {
				const supplyDate = this.source.supplyDate.substring(0, 10)
				let str = "待交货";
				if (new Date(supplyDate).getTime() < new Date(this.$helper.getDay(0, '-')).getTime()) {
					str = "缺货";
				}
				return str;
			}
		},
		mounted() {

		},
		methods: {
			callPhone(tel) {
				uni.makePhoneCall({
					phoneNumber: tel
				})
			},
			goDetail() {
				uni.navigateTo({
					url: '/subPage_supply/orderDetail?type=' + this.index + '&params=' + JSON.stringify(this
						.source)
				})
			},
			goTesting() {
				uni.navigateTo({
					url: '/subPage_testing/index?keywrods=' + this.source.supplierName + "&supplyDate=" + this
						.source.supplyDate.substring(0, 10)
				})
			},
			goReceipt() {
				uni.navigateTo({
					url: '/subPage_receiptOrder/index?keywrods=' + this.source.supplierName + "&supplyDate=" + this
						.source.supplyDate.substring(0, 10)
				})
			},
			empty() {},
			print() {
				this.$showModal({
					title: '打印确认',
					content: '是否要打印供货单？',
					confirmText: '打印',
					cancelText: '取消',
					success: async (res) => {
						if (res.confirm) {
							this.printConfirm()
						}
					}
				})
			},
			async remind() {
				if (!this.source.serviceTel) {
					uni.showToast({
						title: '暂未设置电话'
					})
					return
				}
				this.$showModal({
					title: '提示',
					content: '是否要提醒供应商交货？',
					confirmText: '确定',
					cancelText: '取消',
					success: async (res) => {
						if (res.confirm) {
							const obj = this.supplyWarehouseInfo.schemaList.find(item => item.caption ===
								this.source
								.schemaCaption)
							const data = await this.$api.post('/adm/supply/order/delivery-reminder', {
								reminderInfos: [{
									"mobile": this.source.serviceTel,
									"quantity": this.source.pendingQuantity,
									"endTime": obj.stopReceiveTime || '18:00'
								}]
							}, false)
							if (data.code === 200) {
								uni.showToast({
									title: '已提醒'
								})
							}
						}
					}
				})
			},
			async printConfirm() {
				let row = this.source;
				let stopTime = row.stopDateTime;
				let dateStr = stopTime.split(" ")[1];
				dateStr = dateStr.split(":");
				let hour = dateStr[0];
				let minute = dateStr[1];
				let second = "00";
				let queryStopTime = `${row.supplyDate} ${
				        hour < 10 ? "0" + hour : hour
				      }:${minute}:${second}`;

				const data = await this.$api.post('/adm/supply/record/print', {
					lockEndTime: queryStopTime,
					supplyOrderId: this.source.id
				})
				if (data.code === 200 && data.data.length) {
					let number = data.data[0].lockQuantity;
					if (number > 0) {
						let time = new Date(queryStopTime).getTime() + 20 * 60 * 1000;
						time = new Date(time);
						let timeStr = time.getHours() + ":" + time.getMinutes();
						let content =
							`当前供应商，还有${data.data[0].lockQuantity}个订单尚处于锁单状态，客户可能会取消订单，所以${timeStr}以后才能打印供货单。`;
						// this.$alert(content, "打印提示", {
						// 	confirmButtonText: "我知道了",
						// });
						this.$showModal({
							title: '打印提示',
							content: content,
							confirmText: '我知道了',
							showCancel: false
						})
						return;
					}
					this.donePrint(data.data)
				}
			},
			donePrint(item) {
				const data = item[0];
				const list = [{
						type: 0,
						text: "供货单",
						size: 32,
						align: 1,
						bold: true,
					}, {
						type: 4,
						height: 25
					}, {
						type: 0,
						text: "供货日期：" + data.supplyDate,
						size: 28,
						bold: true
					}, {
						type: 4,
						height: 10
					}, {
						type: 0,
						text: "供货阶段：" + this.source.schemaCaption,
						size: 28,
						bold: true
					}, {
						type: 4,
						height: 10
					},
					{
						type: 0,
						text: "供应商：" + data.supplierName,
						size: 28,
						bold: true
					}, {
						type: 4,
						height: 20
					}, {
						type: 0,
						text: `交货流程：`,
						size: 30,
						bold: true,
					},
					{
						type: 4,
						height: 5
					},
					{
						type: 0,
						text: `1、交货：按照下面提示，前往各停车位进行交货，并由装车工盖章确认`,
						size: 24,
						bold: true
					},
					{
						type: 4,
						height: 5
					}, {
						type: 0,
						text: `2、回单：完成所有交货以后，携带本单据，前往出口处扫码回单。`,
						size: 24,
						bold: true
					}, {
						type: 4,
						height: 30
					},
					{
						type: 0,
						text: "切记：不回单，等于没有交货！！！！",
						size: 28,
						bold: true,
					},
					{
						type: 0,
						text: `共 ${item.length} 个交货单`,
						size: 24,
						bold: true
					},
					{
						type: 0,
						text: `${data.printTime}`,
						size: 24,
						bold: true
					},
					{
						type: 0,
						text: `第${data.printTimes }次打印`,
						size: 24,
						bold: true
					},
					{
						type: 4,
						height: 50
					}
				]
				for (var i = 0; i < item.length; i++) {
					list.push({
						type: 2
					}, {
						type: 4,
						height: 40
					}, {
						type: 0,
						text: `交货单编号：${item[i].code}`,
						size: 24,
						bold: true
					}, {
						type: 4,
						height: 15
					}, {
						type: 0,
						text: `城市仓：${item[i].pickupPointCode} ${item[i].cityName}.${item[i].pickupPointName}`,
						size: 24,
						bold: true
					})
					item[i].vehicleList.forEach((row, index) => {
						let text = `停车位：${row.parkingSpace}  ${row.vehicleCode}`;
						let height = 15;
						if (index > 0) {
							text = `       ${row.parkingSpace}  ${row.vehicleCode}`;
							height = 8;
						}
						list.push({
							type: 4,
							height: height
						}, {
							type: 0,
							text: text,
							size: 24,
							bold: true
						})
					})
					list.push({
						type: 4,
						height: 25
					}, {
						type: 3
					})
					item[i].productList.forEach((row, index) => {
						list.push({
							type: 4,
							height: 25
						}, {
							type: 0,
							text: `${index+1}、${row.productName}`,
							size: 22,
							bold: true
						}, {
							type: 4,
							height: 15
						}, {
							type: 0,
							text: `货号：${row.productCode}`,
							size: 26,
							bold: true
						}, {
							type: 4,
							height: 15
						}, {
							type: 0,
							text: `客户订货：${row.quantity}件`,
							size: 26,
							bold: true
						}, {
							type: 4,
							height: 15
						}, {
							type: 0,
							text: `实际交货：               件`,
							size: 26,
							bold: true
						}, {
							type: 4,
							height: 25
						}, {
							type: 3
						})
					})
					let code = {
						type: 1,
						code: item[i].id
					}

					list.push({
						type: 4,
						height: 25
					}, {
						type: 0,
						text: `装车确认：`,
						size: 26,
						bold: true
					}, {
						type: 4,
						height: 60
					}, {
						type: 6,
						size: 8,
						text: JSON.stringify(code),
						align: 1
					}, {
						type: 4,
						height: 40
					})
				}

				list.push({
					type: 4,
					height: 100
				})
				//console.log("list", list)
				this.$printFn(list, 2)
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.productItem {
		background-color: #ffffff;
		border-radius: 10upx;
		padding: 20upx 30rpx;

		.user-info {
			display: flex;
			align-items: center;
			font-size: 32upx;
			padding: 0 0upx;

			.name {
				font-weight: bold;
				flex: 1;
				color: #222222;
				font-size: 34upx;
			}

			.tel {
				color: #666;
			}

			.icon {
				margin-right: 5upx;
			}
		}

		.region {
			padding: 0 0upx;
			color: #888888;
			margin-top: 10upx;
		}

		.info-wrapper {
			display: flex;
			align-items: center;
			margin: 20upx 0upx;
			border-top: 1upx dashed #e1e1e1;
			border-bottom: 1upx dashed #e1e1e1;
			padding: 20upx 0;

			.item {
				&.flex {
					flex: 1;
				}

				.item-bottom {
					margin-top: 15upx;
					font-weight: bold;

					&.warning {
						color: #f90;
					}
				}

			}
		}

		.delivery-quantity {
			display: flex;
			align-items: center;
			padding: 20upx 0;
			margin-top: 20upx;
			border-top: 1upx solid #efefef;

			.item {
				flex: 1;
				font-size: 30upx;

				&.center {
					text-align: center;
				}

				&.right {
					text-align: right;
				}
			}
		}

		.btn-wrapper {
			display: flex;
			align-items: center;
			justify-content: flex-end;
			margin-top: 10upx;

			.date {
				font-size: 34upx;
				font-weight: bold;
				color: #555BFF;
				flex: 1;
			}

			.tip {
				font-size: 32rpx;
				color: #666;
			}

			.btn {
				padding: 14upx 30upx;
				background-color: #ffffff;
				border: 1upx solid #bababa;
				border-radius: 10upx;
				font-size: 28upx;
				margin-left: 15upx;

				&.primary {
					color: #ffffff;
					background-color: #555BFF;
					border-color: #555BFF;
				}

				&.disabled {
					color: #ffffff;
					background-color: #aaaaaa;
					border-color: #aaaaaa;
				}
			}
		}
	}
</style>